package com.xiaolong.web.serializer;

import com.alibaba.fastjson.serializer.CollectionCodec;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.JavaBeanSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerialContext;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.TreeSet;

public class SetCollectionCodec extends CollectionCodec {
    public static final SetCollectionCodec INSTANCE = new SetCollectionCodec();

    public SetCollectionCodec() {
    }

    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
        SerializeWriter out = serializer.out;
        if (object == null) {
            out.writeNull();
        } else {
            boolean isWriteClassName = out.isEnabled(SerializerFeature.WriteClassName);
            Collection<?> collection = (Collection) object;
            SerialContext context = serializer.getContext();
            serializer.setContext(context, object, fieldName, 0);
            if (isWriteClassName) {
                if (HashSet.class.isAssignableFrom(collection.getClass())) {
                    out.append("Set");
                } else if (TreeSet.class == collection.getClass()) {
                    out.append("TreeSet");
                }
            }

            try {
                int i = 0;
                out.append('[');
                Iterator var11 = collection.iterator();

                while (var11.hasNext()) {
                    Object item = var11.next();
                    if (i++ != 0) {
                        out.append(',');
                    }

                    if (item == null) {
                        out.writeNull();
                    } else if (item.getClass() == Integer.class) {
                        out.writeInt((Integer) item);
                    } else if (item.getClass() == Long.class) {
                        out.writeString(String.valueOf(item));
                        if (isWriteClassName) {
                            out.write(76);
                        }
                    } else {
                        ObjectSerializer itemSerializer = serializer.getObjectWriter(item.getClass());
                        boolean enabled = SerializerFeature.isEnabled(features, SerializerFeature.WriteClassName);
                        Type elementType = !isWriteClassName && !enabled ? null : TypeUtils.getCollectionItemType(fieldType);
                        if (enabled && itemSerializer instanceof JavaBeanSerializer) {
                            JavaBeanSerializer javaBeanSerializer = (JavaBeanSerializer) itemSerializer;
                            javaBeanSerializer.writeNoneASM(serializer, item, i - 1, elementType, features);
                        } else {
                            itemSerializer.write(serializer, item, i - 1, elementType, features);
                        }
                    }
                }

                out.append(']');
            } finally {
                serializer.setContext(context);
            }
        }
    }
}

